package com.kk.init;

import com.kk.dao.*;
import com.kk.model.*;
import lombok.extern.log4j.Log4j2;
import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 初始化数据
 *
 * @author: kevin
 * @date: 2018-10-14 20:55
 */
@Component
@Log4j2
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuRelDao roleMenuRelDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private ContentDao contentDao;

    @Value("${spring.jpa.hibernate.ddl-auto}")
    private String ddl_auto;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("===服务启动后，初始化数据等操作===");
        if (StringHelper.isNotEmpty(ddl_auto) && "create".equals(ddl_auto)) {
            User user1 = new User("admin", "admin", "admin");
            User user2 = new User("test", "test", "test");
            User user3 = new User("huangrun", "hr888888", "黄润");
            User user4 = new User("baidu", "baidu888888", "百度");
            Role role1 = new Role("超级管理员");
            Role role2 = new Role("普通用户");
            Set<Role> roleSet1 = new HashSet<>();
            Set<Role> roleSet2 = new HashSet<>();
            roleSet1.add(role1);
            roleSet2.add(role2);
            user1.setRoles(roleSet1);
            user2.setRoles(roleSet2);
            user3.setRoles(roleSet1);
            user4.setRoles(roleSet2);
            userDao.saveAll(Arrays.asList(user1, user2, user3, user4));

            Menu menu1 = new Menu("系统管理", 1, "#");
            Menu menu11 = new Menu("用户管理", 2, "/mgr");
            Menu menu12 = new Menu("角色管理", 2, "/role");
            Menu menu2 = new Menu("内容管理", 1, "/content");
            menu11.setParent(menu1);
            menu12.setParent(menu1);
            menu1.setIcon("fa-user");
            menu2.setIcon("fa-file");
            menuDao.saveAll(Arrays.asList(menu1, menu11, menu12, menu2));

            RoleMenuRel roleMenuRel1 = new RoleMenuRel(role1, menu1);
            RoleMenuRel roleMenuRel2 = new RoleMenuRel(role1, menu11);
            RoleMenuRel roleMenuRel3 = new RoleMenuRel(role1, menu12);
            RoleMenuRel roleMenuRel4 = new RoleMenuRel(role1, menu2);
            RoleMenuRel roleMenuRel5 = new RoleMenuRel(role2, menu2);
            roleMenuRelDao.saveAll(Arrays.asList(roleMenuRel1, roleMenuRel2, roleMenuRel3, roleMenuRel4, roleMenuRel5));

            //初始化content数据
            String address = "厦门市集美区灌口镇安仁大道1701号";
            String aboutUs = "是通过厦门市有关部门审核，经工商行政管理局批准的专业公路货物运输公司。从事货运多年，实力雄厚，设有大型仓库，拥有一批经验丰富、素质良好的专业人员。公司引进科学管理规范制度，和质量保证。…";
            String content1 = "回程车整车全国往返运输服务";
            String content2 = "公司长期提供";
            String content3 = "广东、广州、佛山、东莞、深圳、北京、上海、天津、山东、安徽、浙江、广西、海南、江苏、湖南、湖北、贵州、四川、河南、云南、福建、辽宁、山西等地到全国各地区都有源.";
            String content4 = "各种车型、箱车、平板车、高栏车、超低板车。4.2米、6.8米、7.6米、9.6米、13米、17.5米车！";
            String content5 = "回程车往返整车运输服务，价格绝对优惠！！！";
            String content6 = "公司承诺所有合作车辆都持有有效证件、所有司机信息都有备案，各工厂商家可以放心使用。运费付款方式多元化，可以到付、打款、回单付都可以。";
            String content7 = "价格便宜、安全有保障！";
            String content8 = "如果是整车货物，用回程车运输，价格要便宜20％左右。而且回程车装好货就发车，不用中转、再装卸，同一部车完成提货、长途运输，送货，既避免了货物多次转车造成的货损又缩短了到货时间。";
            List<Nav> navList = new ArrayList<>();
            //"首页,大件运输,轿车运输,物流专线,业务范围,车辆展示"
            Nav  nav1 = new Nav("首页","index.html", 1);
            Nav  nav2 = new Nav("大件运输","packageTran.html",2);
            Nav  nav3 = new Nav("轿车运输","carTran.html",3);
            Nav  nav4 = new Nav("物流专线","network.html",4);
            Nav  nav5 = new Nav("业务范围","service.html",5);
            Nav  nav6 = new Nav("车辆展示","showcar.html",6);
            navList.addAll(Arrays.asList(nav1, nav2, nav3, nav4, nav5, nav6));
            Content content = new Content("厦门市广杰顺物流有限公司", "厦门市广杰顺物流有限公司", address, "13003936060", navList, "", aboutUs, content1, content2, content3, content4, content5, content6, content7, content8);
            contentDao.save(content);
            log.info("初始化数据完成");
        } else {
            log.info("ddl-auto：" + ddl_auto + "不是create操作，不进行初始化数据");

        }

    }
}
